package com.pdd.mall.controller;

import com.pdd.mall.common.api.CommonResult;
import com.pdd.mall.entity.Merchant;
import com.pdd.mall.entity.Review;
import com.pdd.mall.service.MerchantService;
import com.pdd.mall.service.ReviewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

/**
 * 商家评价控制器
 */
@RestController
@RequestMapping("/merchant/reviews")
public class MerchantReviewController {

    @Autowired
    private ReviewService reviewService;

    @Autowired
    private MerchantService merchantService;

    /**
     * 获取订单评价
     */
    @GetMapping("/order")
    public CommonResult<Review> getOrderReview(
            @RequestParam String orderNo,
            @RequestParam Long productId) {
        Review review = reviewService.getOrderReview(orderNo, productId);
        // 即使评价不存在也返回成功，只是数据为null
        return CommonResult.success(review);
    }

    /**
     * 获取当前登录商家ID
     */
    private Long getCurrentMerchantId() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String username = authentication.getName();
        Merchant merchant = merchantService.getMerchantByUsername(username);
        return merchant != null ? merchant.getId() : null;
    }
}
